//
//  NewAnalysisContainerViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/11/24.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit

class NewAnalysisContainerViewController: BaseViewController,UIPageViewControllerDelegate,UIPageViewControllerDataSource {

    func endDate() -> String {
        let new = self.ctls[0] as! NewAnalysisViewController
        return new.selectedDate
    }
    
    public let page = UIPageViewController.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
    
    var ctls:[UIViewController] = []
    
    let calendarLabel = UILabel()
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        
        if let idx = ctls.index(of: viewController) {
            
            if idx > 0 {
                
                return ctls[idx - 1]
                
            } else {
                
                return nil
                
            }
        }
        return nil
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        
        if let idx = ctls.index(of: viewController) {
            
            if idx < ctls.count - 1 {
                
                return ctls[idx + 1]
                
            } else {
                
                return nil
                
            }
        }
        
        return nil
    }
    
    
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if completed {
            
            let vc = pageViewController.viewControllers![0]
            
            if let index = ctls.index(of: vc) {
                
                segcontrol.selectedSegmentIndex = index
                segcontrol.changeline()
                self.change(seg: segcontrol)
            }
            
        }
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle{
        get{
            return .lightContent
        }
    }
    
    
    var backcolor: UIColor?
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationItem.hidesBackButton = true
        if self.navigationController != nil {
            if let index = self.navigationController?.children.firstIndex(of: self) {
                if index > 0 {
                    let back = UIBarButtonItem.init(image: UIImage.init(named: "icon_nav_whiteback")!.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(popobjc))
                    back.tintColor = .white
                    self.navigationItem.leftBarButtonItem = back
                } else {
                    self.navigationItem.leftBarButtonItem = nil
                }
            }
        }
        backcolor = self.navigationItem.leftBarButtonItem?.tintColor
//    self.navigationController?.navigationBar.setBackgroundImage(UIColor.hex("2b89df").image(size: CGSize(width: UIScreen.main.bounds.size.width, height: 64)), for: .default)
    }
    
    let segcontrol = AccompanySegmentControl.init(items: ["经营看板","统计分析"])
    
    override func viewDidLoad() {
        super.viewDidLoad()
        segcontrol.selectedSegmentIndex = 0
        segcontrol.tintColor = UIColor.clear
        segcontrol.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white,NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)], for: .selected)
        segcontrol.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.init(red: 1, green: 1, blue: 1, alpha: 0.5),NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)], for: .normal)
        segcontrol.addTarget(self, action: #selector(change(seg:)), for: .valueChanged)
        self.navigationItem.titleView = segcontrol
//    self.navigationController?.navigationBar.setBackgroundImage(UIColor.hex("2b89df").image(size: CGSize(width: UIScreen.main.bounds.size.width, height: 64)), for: .default)
        page.delegate = self
        
        page.dataSource = self
        
        for view in page.view.subviews {
            if let v = view as? UIScrollView {
                v.delegate = segcontrol
            }
        }
        
        self.generaright(date: Date())
        
        self.ctls = configViewControllers()
        
        page.setViewControllers([ctls[0]], direction: UIPageViewController.NavigationDirection.forward, animated: true, completion: nil)
        
        self.view.addSubview(page.view)
        
        NotificationCenter.default.addObserver(self, selector: #selector(rshdate(not:)), name: NSNotification.Name("RfhDate"), object: nil)
        for view in self.view.subviews {
            if view is UIScrollView {
                let scroll = view as! UIScrollView
                scroll.bounces = false
            }
        }
        // Do any additional setup after loading the view.
    }
    
    @IBAction func popdate(_ sender: Any) {
        let newanalysis = self.ctls[0] as! NewAnalysisViewController
        newanalysis.showDateAction(1)
    }
    
    @IBOutlet weak var titleLbl: UILabel!
    
    
    func generaright(date: Date) -> Void {
        
    }
    
    @objc func rshdate(not: Notification)->Void{
//        let date = not.object as! Date
//        let image = UIImage(named: "icon_calendar")!
//
//        let waterimg = image.drawTextInImage(text: date.dateLineStr)
//
//        let button = UIBarButtonItem.init(image: waterimg, style: .plain, target: self, action: #selector(rsh))
//
//        self.navigationItem.rightBarButtonItem = button
    }
    
    @objc func rsh()->Void{
//        let newanalysis = self.ctls[0] as! NewAnalysisViewController
//        newanalysis.showDateAction(1)
    }
    
    func configViewControllers()->[UIViewController] {
        let story = UIStoryboard.init(name: "Main", bundle: Bundle.main)
        let newana = story.instantiateViewController(withIdentifier: "NewAnalysisViewController") as! NewAnalysisViewController
        newana.father = self
        let analys = story.instantiateViewController(withIdentifier: "AnalysisViewController") as! AnalysisViewController
        analys.father = self
        return [newana,analys]
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationItem.hidesBackButton = true
        if self.navigationController != nil {
            if let index = self.navigationController?.children.firstIndex(of: self) {
                if index > 0 {
                    let back = UIBarButtonItem.init(image: UIImage.init(named: "icon_nav_back")!.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(popobjc))
                    back.tintColor = .white
                    self.navigationItem.leftBarButtonItem = back
                } else {
                    self.navigationItem.leftBarButtonItem = nil
                }
            }
        }
//        self.navigationController?.navigationBar.tintColor = backcolor
//    self.navigationController?.navigationBar.setBackgroundImage(UIColor.white.image(size: CGSize(width: UIScreen.main.bounds.size.width, height: 64)), for: .default)
    }
    
    
    override func viewWillLayoutSubviews() {
        
        super.viewWillLayoutSubviews()
        
        page.view.frame = self.view.bounds
    }
    
    
    @objc func change(seg: UISegmentedControl)->Void{
        if seg.selectedSegmentIndex == 0 {
            self.page.setViewControllers([self.ctls[0]], direction: .reverse, animated: true, completion: nil)
        } else {
            self.page.setViewControllers([self.ctls[1]], direction: .forward, animated: true, completion: nil)
            
        }
    }
    
    deinit {
        if let nav = self.navigationController?.navigationBar.viewWithTag(10001) {
            nav.removeFromSuperview()
        }
    }
}
